package com.demo.po;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * User model.

将表结构放在此，消除记忆负担
mysql> desc blog;
+---------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| username | varchar(200) | NO   |     | NULL    |                |
| password | mediumtext   | NO   |     | NULL    |                |
| picadd   | mediumtext   | NO   |     | NULL    |                |
| posL     | mediumtext   | NO   |     | NULL    |                |
| posR     | mediumtext   | NO   |     | NULL    |                |
| pictype  | mediumtext   | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

数据库字段名建议使用驼峰命名规则，便于与 java 代码保持一致，如字段名： userId
 */
@SuppressWarnings("serial")
public class User extends Model<User> {
	public static final User dao = new User();
	
	/**
	 * 所有 sql 写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<User> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from user order by id asc");
	}
	
	public void delete(String username){
		List<Record> list = Db.find("select * from user where username="+username);
		for(Record r:list){
			deleteById(r.get("id"));
		}
	}
	
	public List<User> findUser(String username){
		List<User> list = this.find("select * from user where username=?",username);
		return list;
	}
	
	public List<Record> findUser(){
		List<Record> list = Db.find("select * from user");
		return list;
	}
	public static void main(String[] args) {
		User user = new User();
		user.set("username", "zhangsan");
		System.out.println(user.save());
		
	}
}
